package test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import java.lang.reflect.InvocationTargetException;

import org.junit.Before;
import org.junit.Test;

import calculator.Calculator;
import calculator.CalculatorProxy;
import calculator.IntParser;
import calculator.NumberParser;

public class CalculatorProxyTest2 {

	private NumberParser parser;
	private Calculator calculator;
	private CalculatorProxy proxy;
	private IntParser realParser;
	private Calculator realCalculator;
	private CalculatorProxy realCalculatorProxy;

	@Before
	public void setUp() {
		parser = mock(IntParser.class);
		calculator = mock(Calculator.class);
		proxy = new CalculatorProxy(calculator, parser);
		realParser = new IntParser();
		realCalculator = new Calculator();
		realCalculatorProxy = new CalculatorProxy(realCalculator, realParser	);
	}

	@Test
	public void add7and12ParseNumbers() throws NoSuchMethodException,
			IllegalAccessException, InvocationTargetException {

		proxy.calculate("add", "7", "12");

		verify(parser).parse("7");
		verify(parser).parse("12");
	}

	@Test
	public void add7and12invokeCalculator() throws NoSuchMethodException,
			IllegalAccessException, InvocationTargetException {
		
		new CalculatorProxy(calculator, realParser).calculate("add", "7", "12");
		verify(calculator).add(7, 12);
	}

	@Test
	public void add7and12Result() throws NoSuchMethodException,
			IllegalAccessException, InvocationTargetException {
				
		assertEquals(realCalculator.add(7, 12), 
				realCalculatorProxy.calculate("add", "7", "12"));
	}

	@Test
	public void times45ParseNumbers() throws NoSuchMethodException,
			IllegalAccessException, InvocationTargetException {
		proxy.calculate("times", "4", "5");
		verify(parser).parse("4");
		verify(parser).parse("5");
	}

	@Test
	public void times45Calculate() throws NoSuchMethodException,
			IllegalAccessException, InvocationTargetException {
		proxy = new CalculatorProxy(calculator, realParser);
		proxy.calculate("times", "4", "5");
		verify(calculator).times(4,5);
	}
	
	@Test
	public void times45Result() throws NoSuchMethodException,
			IllegalAccessException, InvocationTargetException {
		assertEquals(realCalculator.times(4, 5), realCalculatorProxy.calculate("times", "4", "5"));
		
		
	}
	
	@Test(expected=java.lang.NoSuchMethodException.class)
	public void unsupportedOperation( ) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {		
		realCalculatorProxy.calculate("modus", "2", "3");
	}
	
	@Test
	public void divide8by2Result() {
//		assertEquals(calculator.drealCalculatorProxy.calculate("divide", "8", "2")
	}
}
